import * as lib from '../../../components/aotoo/lib/index'
const dateInfoHooks = lib.hooks('date-info', true)

export default function({tapDay, today, funButtons, record, caseKey}){
  const that = this
  const key = caseKey

  funButtons.dot = funButtons.dot.map(item=>{
    if (item.aim) {
      item.aim = item.aim + '?key='+key
    }
    return item
  })

  // funButtons.dot.shift()

  let   overjobTotal = 0
  const modalData = {
    total: 0,
    slider: 0,
    difftime: 0,  // 加班减去调休的剩余时间
  }

  const alertsData = dateInfoHooks.getItem('case-alerts')
  const dayoff = alertsData.dayoff
  const overjob = alertsData.overjob

  if (overjob) {
    overjob.forEach(item=>{
      overjobTotal += (item.slider||0)
    })
  }

  if (dayoff) {
    dayoff.forEach(item=>{
      modalData.total += (item.slider||0)
    })
  }

  modalData.difftime = overjobTotal - modalData.total

  if (record) {
    modalData.slider = record.slider||0
  }

  const title = `${tapDay.month}月${tapDay.day}日（${tapDay.lunarDate.ncWeek}）`
  const content = {body: [title], date: tapDay.timestr, itemClass: 'dayoff', ...funButtons}

  function beforeCallback({createCaseCard}){
    let dayoffTime = 0
    wx.modalx.showModal({
      content: {
        title: [
          '累计调休', 
          {$$id: 'pop_show_addJobTime', title: `${modalData.total}`, itemClass: 'inner-content-case-title-count'}, 
          {$$id: 'pop_show_diffTime', title: `${modalData.difftime}`, itemClass: 'inner-content-case-title-count-1'}, 
        ], 
        body: [
          // `薪水日为每月 ${today.day} 号`, 
          `当前日期：${tapDay.timestr}${tapDay.timestr === today.timestr ? '(今天)':''}`, 
          {
            itemClass: 'inner-content-case-body',
            title: '调休时长:',
            "@slider": {
              min: 0,
              max: 20,
              value: modalData.slider,
              "block-size": 30,
              "show-value": true,
              "block-color": 'rgb(197, 197, 197, 0.3)',
              bindchange: 'changeTimmer'
            },
          },

          // '帮助：在日历中选中日期，添加调休记录',
          
          {
            title: '我的调休记录', 
            itemClass: 'pop-case-scroll-bottom', 
            aim(){
              wx.modalx.closeModal()
              that.restoreCaseButton()
              wx.pageScrollTo({
                selector: '#pits-list-container',
                duration: 300,
                success(){
                  that.listCaseType('dayoff')
                }
              })
            }}, 
        ],
        methods: {
          changeTimmer(e, param, inst){
            dayoffTime = e.detail.value
            modalData.total += (dayoffTime - modalData.slider)
            modalData.slider = dayoffTime
            modalData.difftime = overjobTotal - modalData.total
            content.body[1] = '调休'+dayoffTime+'小时'

            const showZone = this.activePage.getElementsById('pop_show_addJobTime')
            showZone && showZone .update({
              title: `${modalData.total}`
            })
            const showDiff = this.activePage.getElementsById('pop_show_diffTime')
            showDiff && showDiff .update({
              title: `${modalData.difftime}`
            })
          }
        },
        itemClass: 'inner-content',
      },
      confirmText: '确定',
      cancelText: '取消', 
      height: 60,
      width: 80,
      itemClass: 'popup overjob', 
      mask: false,
      success(res){
        if (res.confirm) {
          createCaseCard({...content, ...modalData})
        }
        that.restoreCaseButton()
      }
    })
  }

  return {key, content, beforeCallback}
}